package site.seean.springboot.kafka.consumer;

import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.util.Optional;

/**
 * @Author: xs
 * @Description: kafka 消费者
 * @Date: 2019/12/04 10:26
 */
@Component
public class Consumer {

    @KafkaListener(topics = {"topic-test1"}, groupId = "g1")
    public void consume(ConsumerRecord<?, ?> record){

        Optional<?> kafkaMessage = Optional.ofNullable(record.value());

        if (kafkaMessage.isPresent()) {

            Object message = kafkaMessage.get();
            System.out.println("g1---->"+record);
            System.out.println("g1---->"+message);

        }

    }

    // 与consume消费相同Topic，但两个Consumer不是同一个Group
    @KafkaListener(topics = {"topic-test1"}, groupId = "g2")
    public void consume2(ConsumerRecord<?, ?> record){

        Optional<?> kafkaMessage = Optional.ofNullable(record.value());

        if (kafkaMessage.isPresent()) {

            Object message = kafkaMessage.get();
            System.out.println("g2---->"+record);
            System.out.println("g2---->"+message);

        }

    }
}
